package com.gx.smart.building.ui.setting.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.SupportFragmentBinding;
import com.gx.smart.building.ui.setting.viewmodel.SupportViewModel;
import com.mylhyl.circledialog.CircleDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class SupportFragment extends BaseMVVMFragmentV2<SupportViewModel> {


    @Override
    public int onBindLayout() {
        return R.layout.support_fragment;
    }

    @Override
    public String getToolbarTitle() {
        return "技术支持";
    }

    @Override
    protected Class<SupportViewModel> onBindViewModel() {
        return SupportViewModel.class;
    }

    @Override
    public void initContentView(ViewGroup root) {
        SupportFragmentBinding supportFragmentBinding = DataBindingUtil.inflate(getLayoutInflater(), onBindLayout(), root, true);
        supportFragmentBinding.setSupportFragment(this);
    }

    public void callPhone(View v) {
        new CircleDialog.Builder()
                .setTitle("拨打电话")
                .setText("您确定要拨打电话吗？")
                .setPositive("确定", v1 -> grantedCallPhonePermission())
                .setNegative("取消", null)
                .show(getActivity().getSupportFragmentManager());
    }


    private void grantedCallPhonePermission() {
        // Storage permission are allowed.
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            Intent intent = IntentUtils.getCallIntent("0571-55555555");
            ActivityUtils.startActivity(intent);
            return;
        }
        AndPermission.with(getActivity())
                .runtime()
                .permission(Permission.CALL_PHONE)
                .onGranted(permissions -> {
                    permissions.contains(Permission.CALL_PHONE);
                    // Storage permission are allowed.
                    grantedCallPhonePermission();
                })
                .onDenied(permissions -> {
                    // Storage permission are not allowed.
                    new CircleDialog.Builder()
                            .setTitle("获取权限")
                            .setText("请允许获取对应的权限，否则不能运行？")
                            .setPositive("确定", null)
                            .show(getActivity().getSupportFragmentManager());
                }).start();
    }
}